package com.zzld.modules.maintenance.action;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.zzld.core.utils.MessageUtils;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.common.dto.CommonJSON;
import com.zzld.modules.maintenance.entity.Matnproduct;
import com.zzld.modules.maintenance.service.IMatnproductService;

@Controller
@RequestMapping("/maintenance/product")
public class MtanProductController {

	@Autowired
	private IMatnproductService productService;
	
	private String listurl="/modules/maintenance/product/productList";
	private String Cruurl="/modules/maintenance/product/productCru";
	
	@RequestMapping(method=RequestMethod.GET)
	public String list(){
		return listurl;
	}
	
	@RequestMapping("/Cru")
	public String cru(){
		return Cruurl;
	}
	
	@RequestMapping("/update/pro/ser")
	@ResponseBody
	public JSONObject ser(HttpServletRequest request
			){
		JSONObject json = new JSONObject();
		try{
			String id = request.getParameter("id");
		if(StringUtils.isNotBlank(id)){
			Matnproduct entity = productService.selectByPrimaryKey(id);
			json.put("productid", entity.getProductid());
			json.put("productname", entity.getProductname());
			json.put("pathId", entity.getPathId());
			json.put("yprice", entity.getYprice());
			json.put("xprice", entity.getXprice());
			json.put("brandId",entity.getBrandId());
			json.put("storeId", entity.getStoreId());
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return json;
	}
	
	@RequestMapping(value="/del/{id}",method=RequestMethod.POST)
	@ResponseBody
	public CommonJSON delbyid(HttpServletRequest request,
			@RequestParam String id){
		CommonJSON json = new CommonJSON();
		try{
			if(StringUtils.isNotBlank(id)){
				if(productService.deleteByPrimaryKey(id)>0){
					json.setMessage(MessageUtils.DELETE_SUCCESS);
					json.setSuccess(true);
				}
			}
		}catch(Exception e){
			json.setMessage(MessageUtils.DELETE_WARNING);
			json.setSuccess(false);
		}
		return json;
	}
	
	@RequestMapping(value="/update/pro",method=RequestMethod.POST)
	@ResponseBody
	public CommonJSON addupdate(HttpServletRequest request,Matnproduct product){
		CommonJSON json = new CommonJSON();
		try{
			if(StringUtils.isBlank(product.getProductid())){
				//新增
				product.setProductid(UuidUtil.uuid().toString());
				product.setCreateTime(new Date());
				if(productService.saveSelective(product)>0){
					json.setMessage(MessageUtils.ADD_SUCCESS);
					json.setSuccess(true);
				}else{
					json.setMessage(MessageUtils.ADD_FAIL);
					json.setSuccess(false);
				}
			}else{
				if(productService.updateByPrimaryKeySelective(product)>0){
					json.setMessage(MessageUtils.EDIT_SUCCESS);
					json.setSuccess(true);
				}else{
					json.setMessage(MessageUtils.EDIT_WARING);
					json.setSuccess(false);
				}
			}
		}catch(Exception e){
			json.setMessage(MessageUtils.NOTICE_RELEASE_ERROR);
			json.setSuccess(false);
		}
		return json;
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public CommonJSON prolist(HttpServletRequest request){
		CommonJSON json = new CommonJSON();
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("productname", request.getParameter("productname"));
		map.put("startime", request.getParameter("startime"));
		map.put("endtime", request.getParameter("endtime"));
		map.put("brandid", request.getParameter("brandid"));
		map.put("pathid", request.getParameter("pathid"));
		
		Integer PageNum = Integer.parseInt(request.getParameter("page"));
		Integer PageSize = Integer.parseInt(request.getParameter("rows"));
		
	
		PageInfo pageinfo =   productService.selectbypage(map, PageNum, PageSize);
		json.setRows(pageinfo.getList());
		json.setTotal(pageinfo.getTotal());
		return json;
	}
}
